package com.sgb.kjwl.view.ui.activity.self.authentication;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAuthBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.AuthEvent;
import com.sgb.kjwl.view.ui.activity.self.userInfo.RegisterActivity3;
import com.sgb.kjwl.viewmodel.self.AuthModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import androidx.databinding.DataBindingUtil;

/**
 * Created by zhaotao on 2019/8/7
 * 新的认证中心首页面
 */
public class AuthActivity extends AppActivity {

    @Inject
    AuthModel viewModel;
    ActivityAuthBinding binding;
    private UserMsgEntity data;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AuthActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(AuthActivity.this, R.layout.activity_auth);
        binding.include.actionBarTitle.setText("认证中心");
        binding.include.actionBarImageLeft.setVisibility(View.VISIBLE);
        binding.include.actionBarImageLeft.setOnClickListener(this);
        boolean linkCompany = SharedPreferenceUtil.getInstance().getBoolean("LINK_COMPANY", false);
        if (linkCompany){
            binding.tvCompanyAuth.setTextColor(getResources().getColor(R.color.color_gray_cbd0d8));
        }else {
            binding.tvCompanyAuth.setTextColor(getResources().getColor(R.color.color_0286DF));
        }
        binding.toCompanyAuth.setOnClickListener(this);
        binding.tvCompanyAuth.setOnClickListener(this);
        binding.toPersonAuth.setOnClickListener(this);
        binding.llCompanyInfo.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.getUserInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.action_bar_image_left:
                finish();
                break;

            case R.id.to_company_auth:
                SupplierAuthActivity.start(AuthActivity.this);
                break;

            case R.id.tv_company_auth:
                boolean linkCompany = SharedPreferenceUtil.getInstance().getBoolean("LINK_COMPANY", false);
                if (!linkCompany){
                    RegisterActivity3.start(this, true);
                }
                break;

            case R.id.to_person_auth:
                CertificationActivity.start(AuthActivity.this);
                break;

            case R.id.ll_company_info:
                if (data == null) return;
                AuthInfoActivity.start(AuthActivity.this, data.isRelevance() ? "SUCCESS" : "NONE");
                //EventBus.getDefault().postSticky(AuthEvent.make().setMessageTag(EventConstant.EVENT_SELF_AUTH_INFO).setMessageData(data.getEnterprise()));
        }
    }

    public void showAuth(UserMsgEntity data) {
        this.data = data;
        binding.rlCompanyAuth.setVisibility(View.VISIBLE);
        binding.rlPersonAuth.setVisibility(View.VISIBLE);

       /* switch (data.getEnterpriseAuth()) {
            case AppConstant.STATUS_NONE_STR:
                binding.ivCompany.setVisibility(View.GONE);
                binding.llCompanyInfo.setVisibility(View.GONE);
                binding.llCompanyUnverified.setVisibility(View.VISIBLE);
                break;

            case AppConstant.STATUS_STANDBY_STR:
                showCompanyAuthInfo(data);
                binding.ivCompany.setImageResource(R.drawable.authing);
                break;

            case AppConstant.STATUS_SUCCESS_STR:
                showCompanyAuthInfo(data);
                binding.ivCompany.setImageResource(R.drawable.authed);
                break;

            case AppConstant.STATUS_FAIL_STR:
                showCompanyAuthInfo(data);
                binding.ivCompany.setImageResource(R.drawable.auth_fail);
                binding.rlAuthFail.setVisibility(View.VISIBLE);
                binding.tvFailReason.setText("失败原因：" + data.getEnterprise().getReason());
                break;
        }*/

        if (data.getPersonalAuth() == 1) {
            binding.ivUser.setVisibility(View.VISIBLE);
            binding.llPresonSuccess.setVisibility(View.VISIBLE);
            binding.llPresonUnverified.setVisibility(View.GONE);

            binding.tvPersonName.setText("姓名：" + data.getRealName());
            binding.tvPersonNo.setText("身份证号：" + data.getIdentityCard());

        } else {
            binding.ivUser.setVisibility(View.GONE);
            binding.llPresonSuccess.setVisibility(View.GONE);
            binding.llPresonUnverified.setVisibility(View.VISIBLE);
        }
    }

    private void showCompanyAuthInfo(UserMsgEntity data) {
        binding.llCompanyUnverified.setVisibility(View.GONE);
        binding.rlAuthFail.setVisibility(View.GONE);
        binding.llCompanyInfo.setVisibility(View.VISIBLE);
        binding.ivCompany.setVisibility(View.VISIBLE);

       /* if (data.getEnterprise() == null) return;
        binding.tvCompanyName.setText("企业名称：" + data.getEnterprise().getCompanyName());
        binding.tvCompanyNo.setText("统一信用代码：" + data.getEnterprise().getCreditCode());
        binding.tvCompanyLegal.setText("法人代表：" + data.getEnterprise().getLegalPerson());
        binding.tvCompanyAddress.setText("企业注册地：" + data.getEnterprise().getCompanyAddress());*/
    }
}
